<template>
  <div class="rank-main" v-loading="loading">
    <div class="rank-heard">
      <el-select v-model="timeVlue" placeholder="Select" style="width: 90px" @change="infoChange">
        <el-option
          v-for="item in timeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-segmented v-model="dataInfoType" :options="options" @change="infoChange" />
    </div>
    <div class="rank-body">
      <el-tabs type="border-card" class="demo-tabs" @tab-change="infoChange">
        <el-tab-pane label="小区排行榜">
          <el-table :data="rankData" style="width: 100%">
            <el-table-column label="排名" prop="rank" width="60" align="center">
              <template #default="scope">
                <div class="rank-index" :class="{ active: scope.row.rank < 4 }">{{
                  scope.row.rank
                }}</div>
              </template>
            </el-table-column>
            <el-table-column label="小区名称" prop="label" align="center" />
            <el-table-column :label="getDataInfoType()" prop="value" width="140" align="center">
              <template #default="scope">
                {{ numberFormat(scope.row.value) }}{{ dataInfoType !== 1 ? '%' : '' }}
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="站点排行榜">
          <el-table :data="rankData" style="width: 100%">
            <el-table-column label="排名" prop="rank" width="60" align="center">
              <template #default="scope">
                <div class="rank-index" :class="{ active: scope.row.rank < 4 }">{{
                  scope.row.rank
                }}</div>
              </template>
            </el-table-column>
            <el-table-column label="站点名称" prop="label" align="center" />
            <el-table-column :label="getDataInfoType()" prop="value" width="140" align="center">
              <template #default="scope">
                {{ numberFormat(scope.row.value) }}{{ dataInfoType !== 1 ? '%' : '' }}
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { numberFormat } from '@/utils/numberFormat.ts'
  import {
    getDashboardGetUseRateByTypeByPeriod,
    getDashboardGetIncomeByTypeByPeriod,
    getDashboardGetProfitRateByTypeByPeriod,
  } from '@/api/controller/charging-controller'
  const rankData = ref()
  const dataType = ref('community')
  const loading = ref(true)
  const dataInfoType = ref(0)
  const timeVlue = ref('month')
  const timeOptions = [
    { label: '近一月', value: 'month' },
    { label: '近一季', value: 'quarter' },
    { label: '近一年', value: 'year' },
  ]
  const options = [
    { label: '使用率', value: 0 },
    { label: '收入额', value: 1 },
    { label: '利润率', value: 2 },
  ]
  onMounted(async () => {
    infoChange('0')
  })
  const infoChange = async (index) => {
    if (index === '0') {
      dataType.value = 'community'
    } else if (index === '1') {
      dataType.value = 'site'
    }
    try {
      loading.value = true
      let res
      switch (dataInfoType.value) {
        case 0:
          res = await getDashboardGetUseRateByTypeByPeriod({
            type: dataType.value,
            period: timeVlue.value,
          })
          rankData.value = res.data
          break
        case 1:
          res = await getDashboardGetIncomeByTypeByPeriod({
            type: dataType.value,
            period: timeVlue.value,
          })
          rankData.value = res.data
          break
        case 2:
          res = await getDashboardGetProfitRateByTypeByPeriod({
            type: dataType.value,
            period: timeVlue.value,
          })
          rankData.value = res.data
          break
      }
    } finally {
      loading.value = false
    }
  }
  const getDataInfoType = () => {
    for (let i = 0; i < options.length; i++) {
      if (options[i].value === dataInfoType.value) {
        return options[i].label
      }
    }
  }
</script>

<style lang="scss" scoped>
  .rank-main {
    background-color: white;
    border: soild 1px #dcdfe6;
    box-shadow: 0px 0px 6px rgba(0, 0, 0, 0.12);
    border-radius: 8px;
    .rank-heard {
      display: flex;
      padding: 16px;
      justify-content: center;
      align-items: center;
      span {
        font-size: 16px;
        font-weight: bold;
        margin-right: 12px;
      }
    }
    .rank-index {
      width: 30px; /* 宽度 */
      height: 30px; /* 高度 */
      line-height: 30px; /* 行高，使文本垂直居中 */
      text-align: center; /* 文本居中 */
      border-radius: 50%; /* 将边框制作成圆形 */
      background-color: rgb(234, 238, 243); /* 背景颜色 */
      color: rgb(84, 84, 84); /* 文本颜色 */
      font-size: 16px; /* 字体大小 */
      font-weight: bold; /* 字体加粗 */
      &.active {
        background-color: rgb(238, 141, 41);
        color: #fff;
      }
    }
  }
  :deep(.el-tabs__nav) {
    display: flex;
    justify-content: center;
    float: none;
  }
</style>
